/*
  作者: 劳兆城
  描述: 创建 Feature 的方法
*/

import Feature from 'ol/feature'
import * as Geom from './geom'
import * as Style from './style'
import * as Util from './util'

// 创建一个 ol.feature
export const createFeature = (options) => new Feature(options)

// 创建一个 icon feature
export const createFeatureIcon = (options = {}) => {
  const { lng, lat, data, img, scale, changeWGS } = options
  let local = [lng, lat]

  if (isNaN(Number(lng)) || isNaN(Number(lat))) {
    return createFeature()
  }

  switch (changeWGS) {
    case 'baidu':
      local = Util.baiduChangeWgs(local)
      break
    case 'gaode':
      local = Util.gaodeChangeWgs(local)
      break
  }

  const feature = createFeature(Geom.createPoint(local))

  feature.set('style', Style.createStyle({
    image: Style.createIcon({
      src: img,
      scale: scale || 0.5
    })
  }))
  feature.set('content', data)

  return feature
}

// 创建一个 icon features
export const createFeaturesIcon = (options = {}) => {
  const { data, img, lngKey, latKey, featureOptions } = options
  const features = []

  data.map(item => {
    const lng = item[lngKey]
    const lat = item[latKey]

    if (
      (!isNaN(Number(lng)) && !isNaN(Number(lat))) &&
      (lng && lat)
    ) {
      const feature = createFeatureIcon({ lng, lat, data: item, img, ...featureOptions })

      features.push(feature)
    }
  })

  return features
}
